<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>取字符串中出现次数最多的字符</title>
</head>

<body>
  <h1>利用散列表，统计字符串中出现次数最多的字符</h1>
  <script>
    //统计字符串中相同字符出现的次数
    var str = "abcdefgaddda";
    var obj = str
      .split('')
      .reduce((p, k) => (p[k]++ || (p[k] = 1), p), {});

    /*遍历这个hash table，获取value最大的key和value*/
    var max = -1;
    var max_key = "";
    var key;
    for (key in obj) {
      if (max < obj[key]) {
        max = obj[key];
        max_key = key;
      }
    }
    document.write(str + "<br>");
    document.write("出现次数最多字符: " + max_key + " 出现次数: " + max);
  </script>
</body>

</html>
